//修改_updateBarStatus，增加动画

import {clamp01, ProgressBar, Size, Sprite, Tween, tween, Vec2, Vec3, warn} from "cc";
import {EDITOR} from "cc/env";

//@ts-ignore
ProgressBar.prototype._updateBarStatus = function () {
    if (this._barSprite) {
        const entity = this._barSprite.node;

        if (!entity) { return; }

        const entTrans = entity._uiProps.uiTransformComp!;
        const entityAnchorPoint = entTrans.anchorPoint;
        const entitySize = entTrans.contentSize;
        const entityPosition = entity.getPosition();

        let anchorPoint = new Vec2(0, 0.5);
        const progress = clamp01(this._progress);
        let actualLenth = this._totalLength * progress;
        let finalContentSize = entitySize;
        let totalWidth = 0;
        let totalHeight = 0;
        switch (this._mode) {
            case ProgressBar.Mode.HORIZONTAL:
                if (this._reverse) {
                    anchorPoint = new Vec2(1, 0.5);
                }

                finalContentSize = new Size(actualLenth, entitySize.height);
                totalWidth = this._totalLength;
                totalHeight = entitySize.height;
                break;
            case ProgressBar.Mode.VERTICAL:
                if (this._reverse) {
                    anchorPoint = new Vec2(0.5, 1);
                } else {
                    anchorPoint = new Vec2(0.5, 0);
                }

                finalContentSize = new Size(entitySize.width, actualLenth);
                totalWidth = entitySize.width;
                totalHeight = this._totalLength;
                break;
            default:
                break;
        }

        // handling filled mode
        if (this._mode === ProgressBar.Mode.FILLED) {
            if (this._barSprite.type !== Sprite.Type.FILLED) {
                warn('ProgressBar FILLED mode only works when barSprite\'s Type is FILLED!');
            } else {
                if (this._reverse) {
                    actualLenth *= -1;
                }

                if (!EDITOR) {
                    Tween.stopAllByTarget(this._barSprite);
                    tween(this._barSprite)
                        .to(0.3, { fillRange: actualLenth }, { easing: "quadOut" }).start()
                } else {
                    this._barSprite.fillRange = actualLenth;
                }

            }
        } else if (this._barSprite.type !== Sprite.Type.FILLED) {
            const anchorOffsetX = anchorPoint.x - entityAnchorPoint.x;
            const anchorOffsetY = anchorPoint.y - entityAnchorPoint.y;
            const finalPosition = new Vec3(totalWidth * anchorOffsetX, totalHeight * anchorOffsetY, 0);

            entity.setPosition(entityPosition.x + finalPosition.x, entityPosition.y + finalPosition.y, entityPosition.z);

            entTrans.setAnchorPoint(anchorPoint);

            Tween.stopAllByTarget(entTrans);
            if (!EDITOR) {
                tween(entTrans)
                    .to(0.1, { contentSize: finalContentSize }, { easing: "quadOut" }).start()
            } else {
                entTrans.setContentSize(finalContentSize);
            }
        } else {
            warn('ProgressBar non-FILLED mode only works when barSprite\'s Type is non-FILLED!');
        }
    }
}